import {message} from 'antd';
export const isSuccess = status => status === 200 || status === '200';
export const parseHeaders = res => {
  const responseCode = res.code;
  const responseMessage = res.message;
  if (responseCode === 401 && res.content) {
    window.location.href = res.content || res.content;
  } else if (responseCode === 403) {
    // gotoUnAuthorized();
  }
  return {responseCode, responseMessage};
}

export default (promise, handlers = {}, silence) => {
  promise.then(res => {
    const response = parseHeaders(res)
    if (isSuccess(response.responseCode)) {
      handlers.success && handlers.success(res.body);
    } else {
      if (silence != true) {
        message.error(res.body.message || '操作失败！');
      }
      handlers.failed && handlers.failed(res.body);
    }
    if (handlers.finally) {
      handlers.finally(res.body);
    }
  }, error => {
    handlers.failed && handlers.failed(error);
    if (handlers.finally) {
      handlers.finally();
    }
    if (silence != true) {
      message.error(error.message || '操作失败！');
    }
  }).catch(error => {
    if (silence != true) {
      promiseUtil.js
      message.error('操作失败！');
    }
    if (handlers.failed) {
      handlers.failed();
    }
    if (handlers.finally) {
      handlers.finally();
    }
  });
}
